package com.asap.demo.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	
	public static final String DATE_FORMAT = "yyyyMMddHHmmss";
	public static final String DATE_FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_FORMAT_SIMPLE = "yyyyMMdd";
	public static final String DATE_FORMAT_YMD = "yyyy-MM-dd";
	public static final String DATE_FORMAT_YM = "yyyyMM";
	public static final String DATE_FORMAT_HM = "HHmm";

	public static final String DATE_FORMAT_HH = "HH";
	public static final String DATE_FORMAT_MM = "mm";
	public static final String DATE_FORMAT_SS = "ss";

	
	/**
	 * 月份加一
	 * 
	 * @param date
	 * @return
	 */
	public static long monthAddFrist(String date) {

		SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT_SIMPLE);

		Calendar ct = Calendar.getInstance();
		try {
			ct.setTime(df.parse(date));

			ct.add(Calendar.MONTH, +1);
			return Long.parseLong(df.format(ct.getTime()));
		} catch (ParseException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return Long.parseLong(date);
	}
	
	public static int getHhMm() {

		Date date = new Date();// yyyyMMddHHmmss
		SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMAT_HH);
		int hour = Integer.valueOf(sf.format(date));
		SimpleDateFormat sfMin = new SimpleDateFormat(DATE_FORMAT_MM);
		int min = Integer.valueOf(sfMin.format(date));

		return hour * 60 + min;

	}

	public static int getHhMmSec() {

		Date date = new Date();// yyyyMMddHHmmss
		SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMAT_HH);
		int hour = Integer.valueOf(sf.format(date));
		SimpleDateFormat sfMin = new SimpleDateFormat(DATE_FORMAT_MM);
		int min = Integer.valueOf(sfMin.format(date));

		SimpleDateFormat sfSec = new SimpleDateFormat(DATE_FORMAT_SS);
		int sec = Integer.valueOf(sfSec.format(date));

		return hour * 60 * 60 + min * 60 + sec;

	}

	/**
	 * 获取今天所在的年月日
	 * 
	 * @param dateFormat
	 *            时间格式如：YYYY-MM-DD 或MM
	 * @return String 当时间的格式串，例如2007-05-11
	 * @see
	 */
	public static String getCurrDate(String dateFormat) {
		Date date = new Date();// yyyyMMddHHmmss
		SimpleDateFormat sf = new SimpleDateFormat(dateFormat);
		return sf.format(date);
	}

	/**
	 * 按YYYYMMDD格式解析日期字符串为日期
	 * 
	 * @param
	 *            ：YYYY-MM-DD 或MM
	 * @param dateStr，日期字符串，例如'20070501'
	 * @return Date 日期
	 */
	public static Date parseDate(String dateStr) {
		if (dateStr == null || dateStr.equals(""))
			return null;
		SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMAT_FULL);
		try {
			return sf.parse(dateStr);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
	/**
	 * 按YYYYMMDD格式解析日期字符串为日期
	 *
	 * @param dateFormat
	 *            时间格式如：YYYY-MM-DD 或MM
	 * @param dateStr，日期字符串，例如'20070501'
	 * @return Date 日期
	 */
	public static Date parseDate(String dateStr, String dateFormat) {
		if (dateStr == null || dateStr.equals(""))
			return null;
		SimpleDateFormat sf = new SimpleDateFormat(dateFormat);
		try {
			return sf.parse(dateStr);
		} catch (Exception ex) {
			return null;
		}
	}

	/**
	 * 将date转成String
	 * 
	 * @param dateStr，日期字符串，例如'20070501'
	 * @param dateFormat
	 *            时间格式如：YYYY-MM-DD 或MM
	 *
	 * @return string
	 */
	public static String parseDateToString(Date dateStr, String dateFormat) {
		if (dateStr == null ){
			dateStr = new Date();
		}
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		try {
			return sdf.format(dateStr);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}

	}

	/**
	 * 获取当前月
	 * 
	 * @return 月份
	 */
	public static String getCurrMonth() {
		Calendar c = Calendar.getInstance();
		return "" + (c.get(Calendar.MONTH) + 1);
	}

	/**
	 * 获取当月天
	 * 
	 * @return 当月天
	 */
	public static String getCurrMonDay() {
		Calendar c = Calendar.getInstance();
		return "" + (c.get(Calendar.DAY_OF_MONTH));
	}

	public static int getCurrDay() {
		int nowDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
		return nowDay;
	}
	/**
	 * 按yyyyMMddHHmmss格式获取当前日期
	 * 
	 * @return
	 */
	public static String longDate() {
		// 得到long类型当前时间
		long longDate = System.currentTimeMillis();
		// new日期对象
		Date date = new Date(longDate);
		// 转换提日期输出格式
		SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
		return dateFormat.format(date).toString();
	}

	/**
	 * 
	 * @return
	 */
	public static String dateInfo() {
		// 得到long类型当前时间
		long longDate = System.currentTimeMillis();
		// new日期对象
		Date date = new Date(longDate);
		// 转换提日期输出格式
		SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_FULL);
		return dateFormat.format(date).toString();
	}
	/**
	 * 
	 * @return
	 */
	public static int gethsDate() {
		// 得到long类型当前时间
		long longDate = System.currentTimeMillis();
		// new日期对象
		Date date = new Date(longDate);
		// 转换提日期输出格式
		SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_HM);

		return Integer.valueOf(dateFormat.format(date).toString());
	}
	
	public static String getData() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date());
	}
	

}
